package com.baiyinet.ui.main;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.baiyinet.R;
import com.baiyinet.biz.PageStatus;
import com.baiyinet.rest.ChannelInfo;
import com.baiyinet.ui.BaseToolbarFragment;
import com.baiyinet.util.IntentUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import java.util.ArrayList;
import java.util.List;

import static com.baiyinet.BuildConfig.ENDPOINT;

/**
 * Created by developer on 18-2-1.
 */

@EFragment(R.layout.frag_home)
public class HomeFragment extends Fragment {

    @ViewById(R.id.frag_home_tab_layout)
    SlidingTabLayout mTabLayout;
    @ViewById(R.id.frag_home_tab_view_pager)
    ViewPager mViewPager;

    @FragmentArg("channelInfo")
    ChannelInfo channelInfo;
    @FragmentArg("pageStatus")
    PageStatus pageStatus;

    @StringRes(R.string.network_disabled)
    protected String strNetworkDisabled;

    private ArrayList<Fragment> mFragmentList = new ArrayList<>();

    @AfterViews
    void initView() {
        ArrayList<ChannelInfo.Channel> channelList = channelInfo.ChannelList;
        String[] titles = new String[channelList.size()];

        for (int i = 0; i < channelList.size(); i ++) {
            ChannelInfo.Channel channel = channelList.get(i);
            mFragmentList.add(CommonWebFragment_.builder().url(channel.Url.substring(7)).build());

            titles[i] = channel.Title;
        }

        mTabLayout.setViewPager(mViewPager, titles, getActivity(), mFragmentList);
        mTabLayout.setCurrentTab(0);
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mFragmentList.get(position).onHiddenChanged(false);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Click(R.id.search_iv_page)
    void toSearchPage() {
        String url = ENDPOINT + "/Home/Catalog";
        PageStatus pageStatus = new PageStatus(url, true, true, this.pageStatus.toolbarBgColor);
        IntentUtils.toNewGeneralActivity(getActivity(), pageStatus);
    }

}
